package com.think.teaching.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.teaching.mapper.ActionTypeMapper;
import com.think.teaching.model.ActionType;
import com.think.teaching.model.Tag;
import com.think.teaching.service.ActionTypeService;

/**
 * 事件类型接口实现层
 * @author 赵兴
 * 2020-11-12
 */
@Service
public class ActionTypeServiceImpl  extends BaseServiceImpl<ActionTypeMapper, ActionType> implements ActionTypeService{
	
	
	@Override
	public ActionType AddActionType(ActionType actionType) {

		if (this.save(actionType)) {
			return actionType;
		}
		return null;
	}
	
	public int GetActionTypeCountByName(String name) {
		//判断是否存在同名事件类型
       	return baseMapper.getSameNameActionTypeNums(name);
	}


	@Override
	public ActionType UpdateActionType(ActionType actionType) {
		if (this.updateById(actionType)) {
			return actionType;
		}
		return null;
	}

	@Override
	public ActionType GetActionType(Integer id,Boolean withoutBan) {
		QueryWrapper<ActionType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return getOne(queryWrapper);
	}

	@Override
	public ActionType SetActionTypeState(Integer id, Integer state) {
		UpdateWrapper<ActionType> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}


	@Override
	public List<ActionType> ListActionTypeSearch(String key,Integer type, Boolean withoutBan) {
		QueryWrapper<ActionType> queryWrapper = new QueryWrapper<>();
		
		queryWrapper.ne("delflag", 1).eq("type", type).like("name", key);	
		
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		queryWrapper.last("limit 0,10");
		return list(queryWrapper);
	}
	
	@Override
	public IPage<ActionType> ListActionTypeWithType(Integer count, Integer page, Integer type,
			Boolean withoutBan) {
		QueryWrapper<ActionType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("type", type);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<ActionType> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}
	
	@Override
	public ActionType GetActionTypeByType(Integer type) {
		QueryWrapper<ActionType> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("type", type);
		return this.getOne(queryWrapper);
	}
}
